package com.klb.service.admin.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.klb.dao.impl.ArticleDaoImpl;
import com.klb.dao.impl.BlockDaoImpl;
import com.klb.model.BbsArticle;
import com.klb.model.BbsBlock;
import com.klb.service.admin.ArticleServiceAdmin;
import com.klb.tools.Tools;

/**
 * Created by fujia on 2016/7/2.
 */
public class ArticleServiceAdminImpl implements ArticleServiceAdmin {
    private ArticleDaoImpl articleDao;
    private BlockDaoImpl blockDao;

    public void setArticleDao(ArticleDaoImpl articleDao) {
        this.articleDao = articleDao;
    }

    public void setBlockDao(BlockDaoImpl blockDao) {
        this.blockDao = blockDao;
    }

    @Override
    public List<BbsArticle> getAllBlockArticles(int blockId) {
        return this.articleDao.getArticlesByBlockId(blockId);
    }

    @Override
    public List<HashMap<String, Object>> getAllArticles() {
        List<HashMap<String, Object>> data = null;

        List<BbsArticle> articles = this.articleDao.getAllArticles();
        if (!Tools.objectIsNullOrNot(articles)){
            data = new ArrayList<>();

            if (!Tools.objectIsNullOrNot(articles)){
                for (BbsArticle article : articles){
                    HashMap<String, Object> map = new HashMap<>();

                    BbsBlock block = this.blockDao.getBlockById(article.getBlockId());

                    map.put("articleId", article.getArticleId());
                    map.put("title", article.getTitle());
                    map.put("date", article.getDateAdded());
                    map.put("block", block.getTitle());

                    data.add(map);
                }
            }
        }

        return data;
    }

}
